/* tripl/error.c 
 * 
 * This file is part of tripl. 
 * 
 * tripl is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * tripl is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with tripl. If not, see <https://www.gnu.org/licenses/>
 */ 




#include <tripl/tripl.h>
#include <tripl/limits.h>

#include <string.h>



int generic_error(char *msg, char *line, int lno) {
	char *buffer = malloc(TRIPL_LINE_MAX);
	char *p;
	int l;

	for (l = 0, p = line; *p != '\n' && l < TRIPL_LINE_MAX; p++, l++) 
		;

	strncpy(buffer, line, l);
	buffer[l] = 0;

	ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_WHITE, BG_BLACK);
	fprintf(stderr, "tripl: %s:%d: ", fname, lno);
	ansi_csi_ed(stderr);

	ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_RED, BG_BLACK);
	fprintf(stderr, "error: ");
	ansi_csi_ed(stderr);

	fprintf(stderr, "%s\n", msg);

	fprintf(stderr, "%5d | ", lno);

	ansi_csi_st(stderr, MODE_LINE, FG_WHITE, BG_BLACK);
	fprintf(stderr, "%s\n", buffer);
	ansi_csi_ed(stderr);

	free(buffer);

	return 1;
}


